<?php
if (!isset($_POST["db_server"])) { 
?>
<h1>Selection Tool (Light Version) Installation</h1>
<form name='install' method='post'>
<table>
<tr><th colspan=2>DataBase configuration</th></tr>
<tr><td>Server</td><td><input type='text' size=30 name='db_server'/></td></tr>
<tr><td>User</td><td><input type='text' size=30 name='db_user'/></td></tr>
<tr><td>Password</td><td><input type='password' size=30 name='db_pass'/></td></tr>
<tr><td>DataBase Name</td><td><input type='text' size=30 name='db_name'/></td></tr>
<tr><td colspan=2 align=center><button type='submit'>Install</button></td></tr>
</table>
<b>Warning:</b> if the database already exists, it will be completely removed.
</form>
<?php
die();
}

$db_server = $_POST["db_server"];
$db_user = $_POST["db_user"];
$db_pass = $_POST["db_pass"];
$db_name = $_POST["db_name"];

$db_config = "<?php\r\n". 
"global \$db_config;\r\n".
"\$db_config = array(\r\n".
"	\"server\"=>\"".$db_server."\",\r\n".
"	\"user\"=>\"".$db_user."\",\r\n".
"	\"password\"=>\"".$db_pass."\",\r\n".
"	\"database\"=>\"".$db_name."\"\r\n".
");\r\n".
"?>\r\n";

$f = fopen("../db_config.inc","w");
fwrite($f, $db_config);
fclose($f);

require_once("../DataBase.inc");

DataBase::remove_database($db_name);
DataBase::create_database($db_name);
require_once("../DB/database.inc");
foreach ($app_fields as $table=>$fields) {
	$query = "CREATE TABLE IF NOT EXISTS `".$table."` (";
	$first = true;
	$key = null;
	foreach ($fields as $name=>$type) {
		if ($first) $first = false; else $query .= ",";
		$query .= "`".$name."` ";
		$type = explode("#", $type);
		if ($type[0]=="KEY") {
			$key = $name;
			array_shift($type);
		}
		switch ($type[0]) {
			case "KEY_AI": $query .= "int(11) NOT NULL AUTO_INCREMENT"; $key = $name; break;
			case "id": $query .= "int(11)"; break;
			case "string":
				if (count($type)>1) 
					$query .= "VARCHAR(".$type[1].")";
				else
					$query .= "TEXT"; 
				break;
			case "date": $query .= "date"; break;
			case "time": $query .= "time"; break;
			case "bool": $query .= "tinyint(1) unsigned DEFAULT '0'"; break;
			case "int": $query .= "int(11)"; break;
			case "double": $query .= "double"; break;
		}
	}
	if ($key <> null)
		$query .= ",PRIMARY KEY (`".$key."`)";
	$query .= ") ENGINE=MyISAM  DEFAULT CHARSET=UTF8;";
	DataBase::query($query);
	if (DataBase::last_error_message() <> 0)
		echo "Error #".DataBase::last_error_message().": ".DataBase::last_error_message()."<br/>";
}

$departments = array(
	array("department_id"=>1, "department_name"=>"Education"),
	array("department_id"=>2, "department_name"=>"Training"),
	array("department_id"=>3, "department_name"=>"Selection"),
	array("department_id"=>4, "department_name"=>"IT Admin"),
	array("department_id"=>5, "department_name"=>"Company"),
	array("department_id"=>6, "department_name"=>"Admin"),
	array("department_id"=>7, "department_name"=>"Direction"),
);
$staff = array(
	
	array("username"=>"nika","password"=>md5("sour"),"first_name"=>"Nika","last_name"=>"SOUR","department_id"=>6),
	array("username"=>"pidor","password"=>md5("chhiv"),"first_name"=>"Pidor","last_name"=>"CHHIV","department_id"=>6),
	array("username"=>"phalla","password"=>md5("ly"),"first_name"=>"Phalla","last_name"=>"LY","department_id"=>6),
	array("username"=>"voleak","password"=>md5("ros"),"first_name"=>"Voleak","last_name"=>"ROS","department_id"=>6),
	array("username"=>"vary","password"=>md5("nim"),"first_name"=>"Vary","last_name"=>"NIM","department_id"=>6),
	array("username"=>"chendadina","password"=>md5("touch"),"first_name"=>"Chendadina","last_name"=>"TOUCH","department_id"=>6),
	array("username"=>"sokan","password"=>md5("khin"),"first_name"=>"Sokan","last_name"=>"KHIN","department_id"=>6),
	array("username"=>"buntha","password"=>md5("un"),"first_name"=>"Buntha","last_name"=>"UN","department_id"=>5),
	array("username"=>"sopheak","password"=>md5("chhun"),"first_name"=>"Sopheak","last_name"=>"CHHUN","department_id"=>5),
	array("username"=>"fabrice","password"=>md5("le matret"),"first_name"=>"Fabrice","last_name"=>"LE MARTRET","department_id"=>5),
	array("username"=>"rita","password"=>md5("lim"),"first_name"=>"Rita","last_name"=>"LIM","department_id"=>5),
	array("username"=>"saroem","password"=>md5("run"),"first_name"=>"Saroem","last_name"=>"RUN","department_id"=>5),
	array("username"=>"sandrine","password"=>md5("nathan"),"first_name"=>"Sandrine","last_name"=>"NATHAN","department_id"=>5),
	array("username"=>"vincent","password"=>md5("drouillard"),"first_name"=>"Vincent","last_name"=>"DROUILLARD","department_id"=>7),
	array("username"=>"thaina","password"=>md5("seang"),"first_name"=>"Thaina","last_name"=>"SEANG","department_id"=>1),
	array("username"=>"samkhann","password"=>md5("khan"),"first_name"=>"Samkhann","last_name"=>"KHAN","department_id"=>1),
	array("username"=>"sokha","password"=>md5("khut"),"first_name"=>"Sokha","last_name"=>"KHUT","department_id"=>1),
	array("username"=>"vandy","password"=>md5("noun"),"first_name"=>"Vandy","last_name"=>"NOUN","department_id"=>1),
	array("username"=>"christophe","password"=>md5("gardella"),"first_name"=>"Christophe","last_name"=>"GARDELLA","department_id"=>1),
	array("username"=>"phoumen","password"=>md5("bun"),"first_name"=>"Phoumen","last_name"=>"BUN","department_id"=>4),
	array("username"=>"vichhay","password"=>md5("nheng"),"first_name"=>"Vichhay","last_name"=>"NHENG","department_id"=>4),
	array("username"=>"seth","password"=>md5("tith"),"first_name"=>"Seth","last_name"=>"TITH","department_id"=>4),
	array("username"=>"ly","password"=>md5("chea"),"first_name"=>"Ly","last_name"=>"CHEA","department_id"=>2),
	array("username"=>"bunthoeun","password"=>md5("kim"),"first_name"=>"Bunthoeun","last_name"=>"KIM","department_id"=>2),
	array("username"=>"leapcheang","password"=>md5("hy"),"first_name"=>"Leapcheang","last_name"=>"HY","department_id"=>2),
	array("username"=>"samedy","password"=>md5("leng"),"first_name"=>"Samedy","last_name"=>"LENG","department_id"=>2),
	array("username"=>"sokrithy","password"=>md5("mom"),"first_name"=>"Sokrithy","last_name"=>"MORM","department_id"=>2),
	array("username"=>"tinay","password"=>md5("vong"),"first_name"=>"Tinay","last_name"=>"VONG","department_id"=>2),
	array("username"=>"mesa","password"=>md5("koeun"),"first_name"=>"Mesa","last_name"=>"KOEUN","department_id"=>2),
	array("username"=>"mengheang","password"=>md5("touch"),"first_name"=>"Meng Heang","last_name"=>"TOUCH","department_id"=>2),
	array("username"=>"lourn","password"=>md5("khiem"),"first_name"=>"Lourn","last_name"=>"KHIEM","department_id"=>2),
	array("username"=>"maneth","password"=>md5("min"),"first_name"=>"Maneth","last_name"=>"MIN","department_id"=>2),
	array("username"=>"sokneang","password"=>md5("sam"),"first_name"=>"Sokneang","last_name"=>"SAM","department_id"=>2),
	array("username"=>"vannak","password"=>md5("pen"),"first_name"=>"Vannak","last_name"=>"PEN","department_id"=>2),
	array("username"=>"guillaume","password"=>md5("le cousin"),"first_name"=>"Guillaume","last_name"=>"LE COUSIN","department_id"=>2),
	array("username"=>"ratha","password"=>md5("mork"),"first_name"=>"Ratha","last_name"=>"MORK","department_id"=>2),
	array("username"=>"sophea","password"=>md5("ou"),"first_name"=>"Sophea","last_name"=>"OU","department_id"=>2),
	array("username"=>"maryline","password"=>md5("chabanis"),"first_name"=>"Maryline","last_name"=>"CHABANIS","department_id"=>2),
	array("username"=>"navy","password"=>md5("sin"),"first_name"=>"Navy","last_name"=>"SIN","department_id"=>2),
	array("username"=>"sophana","password"=>md5("sek"),"first_name"=>"Sophana","last_name"=>"SEK","department_id"=>2),
	array("username"=>"minear","password"=>md5("morn"),"first_name"=>"Minear","last_name"=>"MORN","department_id"=>2),
	array("username"=>"sokhom","password"=>md5("hean"),"first_name"=>"Sokhom","last_name"=>"HEAN","department_id"=>2),
	array("username"=>"sreymom","password"=>md5("meng"),"first_name"=>"Sreymom","last_name"=>"MENG","department_id"=>2),
	array("username"=>"chamroeun","password"=>md5("un"),"first_name"=>"Chamroeun","last_name"=>"UN","department_id"=>2),
);

$staff_roles = array(
	array("username"=>"nika","role"=>"supervisor"),
	array("username"=>"nika","role"=>"interviewer"),
	array("username"=>"pidor","role"=>"supervisor"),
	array("username"=>"pidor","role"=>"interviewer"),
	array("username"=>"phalla","role"=>"supervisor"),
	array("username"=>"phalla","role"=>"observer"),
	array("username"=>"voleak","role"=>"supervisor"),
	array("username"=>"voleak","role"=>"observer"),
	array("username"=>"vary","role"=>"supervisor"),
	array("username"=>"vary","role"=>"observer"),
	array("username"=>"chendadina","role"=>"supervisor"),
	array("username"=>"chendadina","role"=>"observer"),
	array("username"=>"sokan","role"=>"supervisor"),
	array("username"=>"sokan","role"=>"observer"),
	array("username"=>"buntha","role"=>"supervisor"),
	array("username"=>"buntha","role"=>"interviewer"),
	array("username"=>"sopheak","role"=>"supervisor"),
	array("username"=>"sopheak","role"=>"observer"),
	array("username"=>"fabrice","role"=>"supervisor"),
	array("username"=>"rita","role"=>"supervisor"),
	array("username"=>"rita","role"=>"observer"),
	array("username"=>"saroem","role"=>"supervisor"),
	array("username"=>"saroem","role"=>"observer"),
	array("username"=>"sandrine","role"=>"supervisor"),
	array("username"=>"vincent","role"=>"supervisor"),
	array("username"=>"thaina","role"=>"supervisor"),
	array("username"=>"thaina","role"=>"interviewer"),
	array("username"=>"thaina","role"=>"investigator"),
	array("username"=>"thaina","role"=>"application_access"),
	array("username"=>"samkhann","role"=>"supervisor"),
	array("username"=>"samkhann","role"=>"interviewer"),
	array("username"=>"samkhann","role"=>"investigator"),
	array("username"=>"samkhann","role"=>"application_access"),
	array("username"=>"sokha","role"=>"supervisor"),
	array("username"=>"sokha","role"=>"interviewer"),
	array("username"=>"sokha","role"=>"investigator"),
	array("username"=>"sokha","role"=>"application_access"),
	array("username"=>"vandy","role"=>"supervisor"),
	array("username"=>"vandy","role"=>"interviewer"),
	array("username"=>"vandy","role"=>"investigator"),
	array("username"=>"vandy","role"=>"application_access"),
	array("username"=>"christophe","role"=>"supervisor"),
	array("username"=>"christophe","role"=>"application_access"),
	array("username"=>"phoumen","role"=>"supervisor"),
	array("username"=>"phoumen","role"=>"interviewer"),
	array("username"=>"vichhay","role"=>"supervisor"),
	array("username"=>"vichhay","role"=>"observer"),
	array("username"=>"seth","role"=>"supervisor"),
	array("username"=>"seth","role"=>"observer"),
	array("username"=>"ly","role"=>"supervisor"),
	array("username"=>"ly","role"=>"interviewer"),
	array("username"=>"bunthoeun","role"=>"supervisor"),
	array("username"=>"bunthoeun","role"=>"interviewer"),
	array("username"=>"leapcheang","role"=>"supervisor"),
	array("username"=>"leapcheang","role"=>"interviewer"),
	array("username"=>"samedy","role"=>"supervisor"),
	array("username"=>"samedy","role"=>"interviewer"),
	array("username"=>"sokrithy","role"=>"supervisor"),
	array("username"=>"sokrithy","role"=>"interviewer"),
	array("username"=>"tinay","role"=>"supervisor"),
	array("username"=>"tinay","role"=>"interviewer"),
	array("username"=>"mesa","role"=>"supervisor"),
	array("username"=>"mesa","role"=>"interviewer"),
	array("username"=>"mengheang","role"=>"supervisor"),
	array("username"=>"mengheang","role"=>"interviewer"),
	array("username"=>"lourn","role"=>"supervisor"),
	array("username"=>"lourn","role"=>"observer"),
	array("username"=>"maneth","role"=>"supervisor"),
	array("username"=>"maneth","role"=>"observer"),
	array("username"=>"sokneang","role"=>"supervisor"),
	array("username"=>"sokneang","role"=>"observer"),
	array("username"=>"vannak","role"=>"supervisor"),
	array("username"=>"vannak","role"=>"interviewer"),
	array("username"=>"guillaume","role"=>"supervisor"),
	array("username"=>"guillaume","role"=>"application_access"),
	array("username"=>"ratha","role"=>"supervisor"),
	array("username"=>"ratha","role"=>"observer"),
	array("username"=>"sophea","role"=>"supervisor"),
	array("username"=>"sophea","role"=>"observer"),
	array("username"=>"maryline","role"=>"supervisor"),
	array("username"=>"navy","role"=>"supervisor"),
	array("username"=>"navy","role"=>"observer"),
	array("username"=>"sophana","role"=>"supervisor"),
	array("username"=>"sophana","role"=>"observer"),
	array("username"=>"minear","role"=>"supervisor"),
	array("username"=>"minear","role"=>"observer"),
	array("username"=>"sokhom","role"=>"supervisor"),
	array("username"=>"sokhom","role"=>"observer"),
	array("username"=>"sreymom","role"=>"supervisor"),
	array("username"=>"sreymom","role"=>"observer"),
	array("username"=>"chamroeun","role"=>"supervisor"),
	array("username"=>"chamroeun","role"=>"observer"),
);

$programs = array(
	array("program_name"=>"SNA/WEP"),
	array("program_name"=>"DMO"),
);

DataBase::insert_multiples("department", $departments);
if (DataBase::last_error_message() <> 0)
	echo "Error #".DataBase::last_error_message().": ".DataBase::last_error_message()."<br/>";
DataBase::insert_multiples("staff", $staff);
if (DataBase::last_error_message() <> 0)
	echo "Error #".DataBase::last_error_message().": ".DataBase::last_error_message()."<br/>";
DataBase::insert_multiples("staff_roles", $staff_roles);
if (DataBase::last_error_message() <> 0)
	echo "Error #".DataBase::last_error_message().": ".DataBase::last_error_message()."<br/>";
DataBase::insert_multiples("program", $programs);
if (DataBase::last_error_message() <> 0)
	echo "Error #".DataBase::last_error_message().": ".DataBase::last_error_message()."<br/>";
?>
<br/>
Installation done. For security reason, please remove the install folder before to continue.<br/>
<a href="../index.php?logout">Enter application</a>